package com.tust.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tust.app.consts.Constants;
import com.tust.app.domain.TUser;
import com.tust.app.service.TUserService;
import com.tust.app.utils.MD5Utils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private TUserService userService;

    /*展示用户列表*/
    @RequestMapping("/getAllUser")
    public String page2(Integer pn, HttpServletRequest request) {
        try {
            if (pn == null) {
                pn = 1;
            }
            PageHelper.startPage(pn, Constants.mainPageSize);

            List<TUser> userList = userService.selectAll();
            PageInfo<TUser> page = new PageInfo<>(userList);

            request.setAttribute("userList", page);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("展示用户列表出错！");
        }
        return "admin/user/allUser";
    }

    @RequestMapping("/selectByName")
    public String selectByName(String username, HttpServletRequest request) {
        try {
            PageHelper.startPage(Constants.titlePageNum, Constants.titlePageSize);
            List<TUser> userList = userService.selectByUsername(username);
            PageInfo<TUser> pageInfo = new PageInfo<>(userList);
            request.setAttribute("userList", pageInfo);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询新闻标题条数异常！");
        }

        return "admin/user/allUser";
    }

    @RequestMapping("/changeRole")
    public String changeRole(Integer id, Integer roleId, HttpServletRequest request) {
        try {
            TUser user = new TUser();
            user.setId(id);
            user.setRoleId(roleId);
            System.out.println(user);

            userService.updateByPrimaryKeySelective(user);
            PageHelper.startPage(Constants.titlePageNum, Constants.titlePageSize);
            List<TUser> userList = userService.selectAll();
            PageInfo<TUser> pageInfo = new PageInfo<>(userList);
            request.setAttribute("userList", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("更改用户分组异常！");
        }

        return "admin/user/allUser";
    }

    @RequestMapping("/changeState")
    public String changeState(Integer id, Integer state, HttpServletRequest request) {
        try {
            TUser user = new TUser();
            user.setId(id);
            user.setState(state);
            System.out.println("state" + state);
            int i = userService.updateByPrimaryKeySelective(user);
            System.out.println("dasasdyuasgudfasyuA:" + i);
            PageHelper.startPage(Constants.titlePageNum, Constants.titlePageSize);
            List<TUser> userList = userService.selectAll();
            PageInfo<TUser> pageInfo = new PageInfo<>(userList);
            request.setAttribute("userList", pageInfo);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("更改用户分组异常！");
        }

        return "admin/user/allUser";
    }

    @RequestMapping("deleteUser")
    public String deleteUser(Integer id, HttpServletRequest request) {
        try {
            userService.deleteByPrimaryKey(id);
            PageHelper.startPage(Constants.mainPageNum, Constants.mainPageSize);
            List<TUser> userList = userService.selectAll();
            PageInfo<TUser> page = new PageInfo<>(userList);
            request.setAttribute("userList", page);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("删除用户异常！");
        }

        return "admin/user/allUser";
    }


    @RequestMapping("/register")
    public String register(String username, String password, String email, HttpServletRequest request) {
        password = MD5Utils.md5PlusSalt(password);
        TUser tuser = new TUser();
        tuser.setUsername(username);
        tuser.setPassword(password);
        tuser.setEmail(email);
        TUser u = userService.checkreg(username);
        if (u == null) {

            userService.insertSelective(tuser);
            System.out.println("注册成功");
            request.setAttribute("username", username);

        } else {
            System.out.println("注册失败");
            return "home";
        }
        return "forward:/owner";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.removeAttribute("username");
        request.getSession().removeAttribute("username");
        return "forward:/";
    }

    @RequestMapping("/owner")
    public String owner(HttpServletRequest request) {
        String username = (String) request.getSession().getAttribute("username");
        if (username == null) {
            return "forward:/checkLogin";
        }
        return "frontPage/home";
    }

    @RequestMapping("/update")
    public String update(String email, String password, HttpServletRequest request) {
        password = MD5Utils.md5PlusSalt(password);
        String username = (String) request.getSession().getAttribute("username");
        TUser user = userService.checkreg(username);
        if (user.getEmail().equals(email)) {
            user.setPassword(password);
            userService.updateByPrimaryKeySelective(user);
        }
        return "forward:/owner";
    }

    @PostMapping("/changeKey")
    public String changeKey(@RequestParam("username") String username, @RequestParam("password") String password, HttpServletRequest request) {
        password = MD5Utils.md5PlusSalt(password);
        TUser user = userService.checkreg(username);
        user.setPassword(password);
        userService.updateByPrimaryKeySelective(user);
        return "admin/home";
    }
}
